package com.study.pool;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPool
{
    public static void main(String[] args)
    {
//        ExecutorService executor = Executors.newFixedThreadPool(5);
       /* ExecutorService executor = Executors.newSingleThreadExecutor();
        try
        {
            for (int i = 0; i < 5; i++)
            {
                executor.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " ok");
                });
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            executor.shutdown();
        }*/
        System.out.println(Runtime.getRuntime().availableProcessors());
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 5, 4,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());
        try
        {
            for (int i = 0; i < 10; i++)
            {
                pool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" ok");
                });
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            pool.shutdown();
        }
    }
}
